var home = require("../../utils/util.js");
var url = home.home;
var url2 = home.home2;
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    keywords: [],
    value: "",
    color: "color",
    activeKey: 1,
    page: 1, //分页
    type: 1,
    cate: [],
    listData: [], //列表数据
    userId: home.getKey('userId'), //会员id
    cate_id1: '', //分类id
    cate_id2: '', //分类id
    state: wx.getStorageSync('state'),
    states: false,
    btnState:false,
    show:false,//控制下拉列表的显示隐藏，false隐藏、true显示
    selectData:['商品','次卡','预约'],//下拉列表的数据
    index:0,//选择的下拉列表下标
    shom: true,
    bgcolor: home.getKey('bgcolor'),
  },

  // 点击下拉显示框
  selectTap(){
    this.setData({
    show: !this.data.show
    });
  },
  // 点击下拉列表//https://www.freesion.com/article/57681437838/
  optionTap(e){
    let Index=e.currentTarget.dataset.index;//获取点击的下拉列表的下标
    this.setData({
    index:Index,
    type:Index+1,
    show:!this.data.show
    });
    this.getShopList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  selectitem: function (l, e) {
    console.log(e), (this.tval = l >= 0 ? e.value : "");
  },

  onSearchHandle(event){
    this.setData({ value: event.detail.value });
  },
  // onSearchCi(e){
  //   this.data.index = e.currentTarget.dataset['index'];
  //   console.log(this.data.index)
  //   wx.navigateTo({
  //     url: '/pages/search/list/index?keywords=' + this.data.index,
  //   })
  // },

  onSearchSubmit(){
    if(this.data.value.length <= 0){
      home.wxMessage('请输入关键字', 'error');
    }else{
      this.setData({
        value:this.data.value
      })
      this.getShopList();
    }
  },


  //获取列表
  getShopList() {
    let requestUrl = '';
    let groupId = '';
    if (this.data.type == 1) {
      requestUrl = url + 'app/listCommodity2'
      groupId = 0;//搜索请求接口不再判断分类id
    } else if (this.data.type == 2) {
      requestUrl = url + 'app/listProject2';
      groupId = 0;//搜索请求接口不再判断分类id
    } else {
      requestUrl = url + 'app/searchProjectByName';//预约列表接口
      groupId = 0;//搜索请求接口不再判断分类id
    }
    let that = this;
    wx.request({
      url: requestUrl,
      data: {
        storeId: home.getKey('storeId'),
        pageNo: that.data.page,
        groupId: groupId,
        memberId: home.getKey('userId'),
        keyword: that.data.value
      },
      method: 'POST',
      header: {
        "content-type": "application/json", // 默认
      },
      success: function (res) {
        if (res.data.code == 200) {
          let listData = that.data.listData;
          if (that.data.page == 1) {
                for (let i = 0; i < res.data.result.length; i++) {
                  let pics = '';
                  if (that.data.type == 1) {
                    res.data.result[i].titleName = res.data.result[i].tradeName;
                    res.data.result[i].desc = res.data.result[i].specification;
                    res.data.result[i].price = res.data.result[i].productPrice;
                    if (res.data.result[i].productPic) {
                      pics = res.data.result[i].productPic.split(',');
                      if (pics != '') {
                        res.data.result[i].pic = 'https://kyz.fbhkj.com//' + pics[0];
                      }
                    }
                  } else if (that.data.type == 2) {
                    res.data.result[i].titleName = res.data.result[i].projectName;
                    res.data.result[i].desc = res.data.result[i].itemDescription;
                    res.data.result[i].price = res.data.result[i].projectPrice;
                    if (res.data.result[i].projectPic) {
                      pics = res.data.result[i].projectPic.split(',');
                      if (pics != '') {
                        res.data.result[i].pic = 'https://kyz.fbhkj.com//' + pics[0];
                      }
                    }
                  } else {
                    res.data.result[i].titleName = res.data.result[i].name;
                    res.data.result[i].desc = '已预约'+res.data.result[i].createBy+'人次';//这里取的是createBy
                    res.data.result[i].price = 0;
                    res.data.result[i].pic = 'https://kyz.fbhkj.com//' + res.data.result[i].img;
                    res.data.result[i].projectName = res.data.result[i].name;
                    res.data.result[i].count = res.data.result[i].createBy;
                    res.data.result[i].projectId = res.data.result[i].id;
                  }
                }
                listData = res.data.result;
          } else {
              for (let i = 0; i < res.data.result.length; i++) {
                let pics = '';
                if (that.data.type == 1) {
                  res.data.result[i].titleName = res.data.result[i].tradeName;
                  res.data.result[i].desc = res.data.result[i].specification;
                  res.data.result[i].price = res.data.result[i].productPrice;
                  if (res.data.result[i].productPic) {
                    pics = res.data.result[i].productPic.split(',');
                    if (pics != '') {
                      res.data.result[i].pic = 'https://kyz.fbhkj.com//' + pics[0];
                    }
                  }
                } else if (that.data.type == 2) {
                  res.data.result[i].titleName = res.data.result[i].projectName;
                  res.data.result[i].desc = res.data.result[i].itemDescription;
                  res.data.result[i].price = res.data.result[i].projectPrice;
                  if (res.data.result[i].projectPic) {
                    pics = res.data.result[i].projectPic.split(',');
                    if (pics != '') {
                      res.data.result[i].pic = 'https://kyz.fbhkj.com//' + pics[0];
                    }
                  }
                } else {
                  res.data.result[i].titleName = res.data.result[i].name;
                  res.data.result[i].desc = '已预约'+res.data.result[i].createBy+'人次';//这里取的是createBy
                  res.data.result[i].price = 0;
                  res.data.result[i].pic = 'https://kyz.fbhkj.com//' + res.data.result[i].img;
                  res.data.result[i].projectName = res.data.result[i].name;
                  res.data.result[i].count = res.data.result[i].createBy;
                  res.data.result[i].projectId = res.data.result[i].id;
                }
                listData.push(res.data.result[i]);
              }
          }
            if(res.data.result.length>0){
              that.setData({
                listData: listData,
                shom: false
              });
            }else{
              that.setData({
                shom: true
              });
            }
        } else {
          home.wxMessage(res.data.message, 'none');
          that.setData({
             shom: true
          });
        }
      }
    })
  },
  /**
   * 查看详情
   */
  toGoods(e) {
    let goodsId = e.currentTarget.dataset.id;
    let type = this.data.type;
    let info = {};
    for (let i = 0; i < this.data.listData.length; i++) {
      if (this.data.listData[i].id == goodsId) {
        info = this.data.listData[i];
      }        
    }
    if (type == 1) {
      wx.navigateTo({
        url: '../../shop/goods/index?info='+JSON.stringify(info),
      })
    } else if (type == 2) {
      wx.navigateTo({
        url: '../../shop/project/index?info='+JSON.stringify(info),
      })
    } else {
      wx.navigateTo({
        url: '../../shop/yuyue/index?info='+JSON.stringify(info),
      })
    }
  },

})